// import crypto from "crypto-js"
const CryptoJS = require("crypto-js")
// import { getToken } from '@/utils/auth'
let appSecret = process.env.VUE_APP_SECRET ? process.env.VUE_APP_SECRET : 'Z1VMKW1NVSG9A6UDH4ZE9IF7Q1H4BWMD'

export function processAES(password) {
  let str = CryptoJS.enc.Utf8.parse(password)
  let key = CryptoJS.enc.Utf8.parse(appSecret.substring(0, 16))
  let iv = CryptoJS.enc.Utf8.parse(appSecret.substring(16))
  let encryptedData = CryptoJS.AES.encrypt(str, key,
      {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
      })
  return encodeURIComponent(encryptedData)
}

export function signStr(params, oUrl) {
  // console.log(process.env)
  let timestamp = new Date().getTime()
  let publicParams
  // let accessToken = getToken("accessToken")
  let accessToken = "accessToken"
  // console.log(accessToken)
  if (accessToken){
    publicParams = {
      requestId: '999-' + timestamp + '-' + createHexRandom(),
      platform: "H5",
      timestamp: timestamp,
      accessToken: accessToken
    }
  } else {
    publicParams = {
      requestId: '999-' + timestamp + '-' + createHexRandom(),
      platform: "H5",
      timestamp: timestamp
    }
  }

  // console.log(typeof params, params instanceof Array, params)

  // 公共参数与请求参数合并
  let extendParams = new Object()
    // extendParams = Object.assign(publicParams, params)
    for (let k in publicParams){
      extendParams[k] = publicParams[k]
    }

    // 传入的参数为数则不参与重组合并
  if(params instanceof Array == false){
    for (let k in params){
      extendParams[k] = params[k]
    }
  }

  // 字典排序 - 顺
  let sParams = new Object()
  let sdic = Object.keys(extendParams).sort()
  for(let ki in sdic){
    // console.log(sdic[ki]+":"+dic[sdic[ki]]+",");
    sParams[sdic[ki]] = extendParams[sdic[ki]]
  }
  // console.log(sParams)
  // 将参数重新拼接
  let splicingStr
  for(let k in sParams){
    if(!splicingStr){
      splicingStr = '?'
    }
    if(sParams[k] !== ''){
      splicingStr += k+'='+sParams[k] + '&'
    }
  }
  // 删除最后一个字符 '&'
  // splicingStr = splicingStr.slice(0, splicingStr.length - 1)
  // console.log(splicingStr)
  let str = oUrl + splicingStr + appSecret
  // console.log(str)
  // 生成MAC
  let mac = CryptoJS.HmacSHA256(str, appSecret )
  // 生成签名
  let sign = CryptoJS.enc.Base64.stringify(mac)
  publicParams.sign = encodeURIComponent(sign)
  // 公共参数添加sign属性后返回
  return publicParams
  // return encodeURIComponent(sign)
}

//输出 - 顺
export function dicSort(dic) {
  let newObj = new Object
  let sdic = Object.keys(dic).sort()
  for(let ki in sdic){
    // console.log(sdic[ki]+":"+dic[sdic[ki]]+",");
    newObj[sdic[ki]] = dic[sdic[ki]]
  }
  return newObj
}

//输出 - 倒
export function sdicSort(dic) {
  let newObj = new Object
  let sdic = Object.keys(dic).sort(function(a,b){return dic[a]-dic[b]})
  for(let ki in sdic){
    // console.log(sdic[ki]+":"+dic[sdic[ki]]+",");
    newObj[sdic[ki]] = dic[sdic[ki]]
  }
  return newObj
}

export function createHexRandom(){
  let num = '';
  for (let i = 0; i <= 5; i++)
  {
    let tmp = Math.ceil(Math.random()*15);
    if(tmp > 9){
      switch(tmp){
        case(10):
          num+='a';
          break;
        case(11):
          num+='b';
          break;
        case(12):
          num+='c';
          break;
        case(13):
          num+='d';
          break;
        case(14):
          num+='e';
          break;
        case(15):
          num+='f';
          break;
      }
    }else{
      num+=tmp;
    }
  }
  return num
}
